package mvp.com.mvpdemo.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;


import mvp.com.R;
import mvp.com.mvpdemo.MainActivity;
import mvp.com.mvpdemo.presenter.UserLoginPresenter;
import mvp.com.mvpdemo.view.IUserLoginView;

/**
 * Created by YoungerChen on 2016/9/13.
 */
public class LoginActivity extends ActionBarActivity implements IUserLoginView {

    UserLoginPresenter mLoginPresenter = new UserLoginPresenter(this);
    private Button btn_cle;
    private EditText et_username;
    private EditText et_password;
    private Button btn_log;
    private ProgressBar pb;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();
    }

    private void initView() {
        et_username = (EditText) findViewById(R.id.username);
        et_password = (EditText) findViewById(R.id.password);
        btn_log = (Button) findViewById(R.id.btn_log);
        btn_cle = (Button) findViewById(R.id.btn_cle);
        pb = (ProgressBar) findViewById(R.id.pb);

        btn_log.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(LoginActivity.this, "正在登陆。。。", Toast.LENGTH_SHORT).show();
                mLoginPresenter.login();
            }
        });

        btn_cle.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                mLoginPresenter.clear();
            }
        });

    }

    @Override
    public String getUsername() {
        return et_username.getText().toString().trim();
    }

    @Override
    public String getPassword() {
        return et_password.getText().toString().trim();
    }

    @Override
    public void clearUsername() {
        et_username.setText("");
    }

    @Override
    public void clearPassword() {
        et_password.setText("");
    }

    @Override
    public void showLoading() {
        pb.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideLoading() {
        pb.setVisibility(View.GONE);
    }

    @Override
    public void toMainActivity() {

        Toast.makeText(LoginActivity.this, "登录成功！！！", Toast.LENGTH_SHORT).show();
        startActivity(new Intent(this, MainActivity.class));
    }

    @Override
    public void showFailedError() {
        Toast.makeText(LoginActivity.this, "登录失败！！！", Toast.LENGTH_SHORT).show();
    }
}
